package modules.gameTop
{
	import com.feng.FUI;
	
	import modules.GameDispatcher;
	import modules.GameEvent;
	import modules.majiang.view.GameTopComponet;

	public class GameTopManager
	{
		public function GameTopManager()
		{
			addListeners();
		}

		private static var instance:GameTopManager;
		
		/**
		 * 全局唯一实例,游戏顶部布局 
		 */		
		private static var gameTopComponet:GameTopComponet;

		public static function init():void
		{
			logger("游戏页签模块初始化");

			if (instance == null)
			{
				instance = new GameTopManager();
				gameTopComponet = new GameTopComponet(); 
			}
		}

		private function get dispatcher():GameDispatcher
		{
			return GameDispatcher.instance;
		}

		private var buttonObjectList:Array = [];

		/**
		 * 添加侦听器
		 */
		private function addListeners():void
		{
			dispatcher.addEventListener(GameEvent.SHOW_LOBBY, onShowLobby);

			dispatcher.addEventListener(GameEvent.ENTERED_TABLE, onEnteredTable);

			dispatcher.addEventListener(GameEvent.LEAVE_TABLE, onLeaveTable);

			dispatcher.addEventListener(GameEvent.SHOW_TAB_TABLE, onShowTabTable);

			dispatcher.addEventListener(GameEvent.SHOW_TAB_LOBBY, onShowTabLobby);

			dispatcher.addEventListener(GameEvent.ADD_REPLAY_BUTTON, onAddReplayButton);
		}

		private function show():void
		{
			gameTopComponet.x = -2;
			UIAllRefer.popLayer.addChild(gameTopComponet);
			gameTopComponet.visible = false;
		}

		/**
		 * 显示大厅
		 */
		private function onShowLobby(event:GameEvent):void
		{
			show();
			gameTopComponet.addMoreViewButton();
			gameTopComponet.addLobbyButton();
		}
		/**
		 * 进入桌子
		 */
		private function onEnteredTable(event:GameEvent):void
		{
			gameTopComponet.visible = true;
			GameTopComponet.isMoreViewModle = false;
			gameTopComponet.addTableButton(event.data.tableIdStr);
		}

		/**
		 * 离开桌子
		 */ 
		private function onLeaveTable(event:GameEvent):void
		{
			gameTopComponet.onLeaveTable(event.data.tableIdStr);
			if(GameTopComponet.isMoreViewModle == false)
			{
				gameTopComponet.visible = false;
			}
		}
		/**
		 * 显示桌子
		 */
		private function onShowTabTable(event:GameEvent):void
		{
			gameTopComponet.visible = true;
			gameTopComponet.onShowTabTable(event);
		}

		/**
		 * 显示大厅
		 */
		private function onShowTabLobby(event:GameEvent):void
		{
			gameTopComponet.visible = false;
			gameTopComponet.onShowTabLobby(event);
		}

		/**
		 * 添加回放按钮
		 */
		private function onAddReplayButton(event:GameEvent):void
		{
			gameTopComponet.onAddReplayButton(event);
		}
	}
}
